﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication.Controllers
{
    [ApiController]
    [Route("BaseData/[controller]")]
    public class CapController : ControllerBase
    {
        private readonly ICapPublisher _capPublisher;
        public CapController(ICapPublisher capPublisher)
        {
            _capPublisher = capPublisher;
        }//不使用事务

        [HttpGet]
        [Route("pubCap")]
        public bool WithoutTransaction()
        {
            _capPublisher.Publish("helloworld", DateTime.Now);

            return true;
        }

        ////Ado.Net 中使用事务，自动提交
        //[Route("~/adonet/transaction")]
        //public IActionResult AdonetWithTransaction()
        //{
        //    using (var connection = new MySqlConnection(ConnectionString))
        //    {
        //        using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
        //        {
        //            //业务代码

        //            _capBus.Publish("xxx.services.show.time", DateTime.Now);
        //        }
        //    }
        //    return Ok();
        //}

        ////EntityFramework 中使用事务，自动提交
        //[Route("~/ef/transaction")]
        //public IActionResult EntityFrameworkWithTransaction([FromServices] AppDbContext dbContext)
        //{
        //    using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
        //    {
        //        //业务代码

        //        _capBus.Publish("xxx.services.show.time", DateTime.Now);
        //    }
        //    return Ok();
        //}

    }
}
